package com.example.spring_blog.controller;

import com.example.spring_blog.pojo.dataobject.UserInfo;
import com.example.spring_blog.pojo.request.UpdateAvatarRequest;
import com.example.spring_blog.pojo.request.UpdateUserRequest;
import com.example.spring_blog.pojo.request.UserLoginRequest;
import com.example.spring_blog.pojo.request.UserRegisterRequest;
import com.example.spring_blog.pojo.response.UserInfoResponse;
import com.example.spring_blog.pojo.response.UserLoginResponse;
import com.example.spring_blog.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    // 指定接口的实现类是UserServiceImpl，注意使用bean的名称：userServiceImpl
    @Resource(name = "userServiceImpl")
    UserService userService;

    @RequestMapping("/login")
    public UserLoginResponse login(@RequestBody @Validated UserLoginRequest userLoginRequest) { //@RequestBody将JSON字符串转为java对象
        log.info("用户登录，用户名:{}", userLoginRequest.getUserName());
        return userService.checkPassword(userLoginRequest);
    }

    @RequestMapping("/register")
    public Boolean register(@Validated UserRegisterRequest userRegisterRequest) {
        log.info("用户注册，用户名：{}", userRegisterRequest.getUserName());
        return userService.registerUserInfo(userRegisterRequest);
    }

    @RequestMapping("/getUserInfo")
    public UserInfoResponse getUserInfo(@NotNull(message = "参数不能为空") Integer userId) {
        log.info("获取用户信息，用户id:{}", userId);
        return userService.getUserInfo(userId);
    }

    @RequestMapping("/getAuthorInfo")
    public UserInfoResponse getAuthorInfo(@NotNull(message = "参数不能为空") Integer blogId) {
        log.info("获取作者信息，blogId:{}", blogId);
        return userService.getAuthorInfo(blogId);
    }

    @RequestMapping("/update")
    public Boolean update(@RequestBody @Validated UpdateUserRequest updateUserRequest) {
        log.info("用户信息更新，userId:{}", updateUserRequest.getId());
        return userService.updateUserInfo(updateUserRequest);
    }

    @RequestMapping("/uploadAvatar")
    public UserInfoResponse uploadAvatar(@Validated UpdateAvatarRequest updateAvatarRequest) {
        log.info("上传头像，userId:{}", updateAvatarRequest.getId());
        return userService.uploadAvatar(updateAvatarRequest);
    }

}
